package cn.edu.seu.herald.informationplatform.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cn.edu.seu.herald.informationplatform.bean.league.Vote;
import cn.edu.seu.herald.informationplatform.bean.league.VoteItem;
import cn.edu.seu.herald.informationplatform.dao.cookie.LeagueUserSessionCookieAccess;
import cn.edu.seu.herald.informationplatform.dao.cookie.SessionIdNotFound;
import cn.edu.seu.herald.informationplatform.dao.database.VoteDataAccess;
import cn.edu.seu.herald.informationplatform.dao.database.VoteItemDataAccess;
import cn.edu.seu.herald.informationplatform.filter.LeagueSpecified;
import cn.edu.seu.herald.informationplatform.util.filetools.PicTools;

public class VoteConvert extends HttpServlet {

	private static final String SUCCESS_MESSAGE = "操作成功";

	private static final String FAILURE_MESSAGE = "操作失败";
	
	private static final String VOTE_NAME = "voteName";

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//参数："voteName"；
//功能：删除原来的投票活动，新建以参数"voteName"为名的活动；
//		根据链接session获得leagueId
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		try {
		LeagueUserSessionCookieAccess luSessionCookieAccess= new LeagueUserSessionCookieAccess(request);
	
			String voteName = request.getParameter(VOTE_NAME);
			
			String availableNumberString = request.getParameter("availableNumber");
			int availableNumber = Integer.parseInt(availableNumberString);
			if (voteName == null) {
				printResult(response, voteName);
				return;
			}
		
			int leagueId = luSessionCookieAccess.getLeagueId();
			
			VoteDataAccess voteDao = VoteDataAccess.getInstance();
			
			// 更新
			Vote vote = new Vote();
			vote.setLeagueId(leagueId);
			vote.setTitle(voteName);
			vote.setAvailableNumber(availableNumber);
			Vote currentVote = voteDao.getVoteByLeagueId(leagueId);
			if (currentVote == null) {
				voteDao.createNewVote(vote);
			} else {
				VoteItemDataAccess voteItemDataAccess = VoteItemDataAccess.getInstance();
				VoteDataAccess voteDataAccess = VoteDataAccess.getInstance();
				
				Vote preVote = voteDataAccess.getVoteByLeagueId(leagueId);
				int preVoteId = preVote.getId();
				
				Collection<VoteItem> collection = voteItemDataAccess.getVoteItemsByVoteId(preVoteId);
				Iterator iterator = collection.iterator();
				while(iterator.hasNext()){
					VoteItem voteItem = ((VoteItem)iterator.next());
					PicTools.DeletePic(voteItem.getPicture());
				}
				
				voteItemDataAccess.deleteVoteItemsByVoteId(preVoteId);
				
				voteDao.updateVoteByLeagueId(vote);
			}
			
			voteDao.updateVoteByLeagueId(vote);
		
		printResult(response, SUCCESS_MESSAGE);
		
			response.sendRedirect("./polling.jsp?league="+luSessionCookieAccess.getLeagueId());
		}catch (SQLException e) {
			e.printStackTrace();
		} catch (SessionIdNotFound e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void printResult(HttpServletResponse response,
			String msg) throws IOException {
		response.getWriter().println(msg);
	}
	
}
